package com.smart_campus.sys.controller;

import com.smart_campus.sys.common.ActiveUser;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.domain.Notice;
import com.smart_campus.sys.domain.User;
import com.smart_campus.sys.service.NoticeService;
import com.smart_campus.sys.vo.NoticeVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @aytgor 陈鹏
 * @date 2020/6/9 10:42
 * 公告Controller
 */

@RestController
@RequestMapping("Notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * @return ResultObj
     * 公告添加方法
     */
    @RequestMapping("NoticeAdd")
    public ResultObj NoticeAdd(Notice notice) {
        try {
            Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            User user = activeUser.getUser();
            //上面的方法是获取登入的用户
            notice.setOpername(user.getUsername());
            notice.setCreatetime(new Date());
            noticeService.insert(notice);
            return ResultObj.INSERT_NOTICE_YES;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.INSERT_NOTICE_NO;
        }
    }

    /**
     * @param noticeVo
     * @return DataGridView
     * 公告列表
     */
    @RequestMapping("NoticeList")
    public DataGridView NoticeList(NoticeVo noticeVo) {
        return noticeService.selectNotice(noticeVo);
    }

    /**
     * 批量删除公告
     *
     * @param ids
     * @return ResultObj
     * 11
     */
    @RequestMapping("DeleteBatchNotice")
    public ResultObj DeleteBatchNotice(Integer[] ids) {
        try {
            for (Integer id : ids) {
                noticeService.deleteByPrimaryKey(id);
            }
            return ResultObj.DELETE_NOTICE_YES;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_NOTICE_NO;
        }
    }


    /**
     * 删除公告
     *
     * @param id
     * @return ResultObj
     */
    @RequestMapping("DeleteNotice")
    public ResultObj DeleteNotice(Integer id) {
        try {
            noticeService.deleteByPrimaryKey(id);
            return ResultObj.DELETE_NOTICE_YES;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_NOTICE_NO;
        }
    }


    /**
     * 修改公告
     *
     *
     * @param notice
     * @return ResultObJ
     */
    @RequestMapping("UpdateNotice")
    public ResultObj UpdateNotice(Notice notice) {
        try {
            Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            User user = activeUser.getUser();
            //上面的方法是获取登入的用户

            notice.setOpername(user.getUsername());
            noticeService.updateByPrimaryKeySelective(notice);
            return ResultObj.UPDATE_NOTICE_YES;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_NOTICE_NO;
        }
    }
}
